package br.com.ecodif.view.managed;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.xpath.XPathExpressionException;

import org.primefaces.context.RequestContext;
import org.primefaces.event.DragDropEvent;
import org.primefaces.model.menu.DefaultMenuItem;
import org.primefaces.model.menu.DefaultMenuModel;
import org.primefaces.model.menu.DefaultSubMenu;
import org.primefaces.model.menu.MenuModel;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import br.com.ecodif.model.EMMLDocument;
import br.com.ecodif.model.Feed;

@ManagedBean(name="feedMB")
@SessionScoped
public class FeedMB{
	
	public boolean test = false;

	private List<Feed> feeds;
	
	private String xmlFeed;
	
	private List<Feed> selectedFeeds;
	
	private List<EMMLDocument> emmls;
	
	private Long idSelectedFeed;
	
	private MenuModel menuModel;
	
	public FeedMB(){
		feeds = new ArrayList<Feed>();
		feeds.add(new Feed((long) 1,"Feed 1"));
		feeds.add(new Feed((long) 2,"Feed 2"));
		feeds.add(new Feed((long) 3,"Feed 3"));
		feeds.add(new Feed((long) 4,"Feed 4"));
		feeds.add(new Feed((long) 5,"Feed 5"));
		
		
		selectedFeeds = new ArrayList<Feed>();
		
		menuModel = new DefaultMenuModel();
		
		DefaultSubMenu subMenu = new DefaultSubMenu("Feeds");
		subMenu.setId("subMenu");
		
		for(int a = 0 ; a < feeds.size() ; a++){
			DefaultMenuItem menuItem = new DefaultMenuItem(feeds.get(a).getName());
			menuItem.setOnclick("addFeedSelected(this);");
			menuItem.setCommand("#{feedMB.selectedFeed}");
			menuItem.setStyleClass("defaultFont");
			menuItem.setParam("id", feeds.get(a).getId());
			menuItem.setIcon("ui-icon-signal-diag");
			menuItem.setId(""+feeds.get(a).getId());
			System.out.println(menuItem.getId());
			subMenu.addElement(menuItem);
		}
		
		
		menuModel.addElement(subMenu);
//		menuModel.generateUniqueIds();
		 
//		EMMLDocument emml = new EMMLDocument("Teste");
//		emml.PrintAllNodes();

	}

	public void selectedFeed(){
		Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		System.out.println("Value: " + params.get("id"));
		idSelectedFeed = Long.valueOf(params.get("id"));
	}
	
	public List<Feed> getFeeds() {
		return feeds;
	}
	

	public void setFeeds(List<Feed> feeds) {
		this.feeds = feeds;
		
	}

	public void onDrop(DragDropEvent event){
//		Feed feed = (Feed) event.getData();
//		selectedFeeds.add(feed);
		System.out.println("Dropped");
		selectedFeeds.add(new Feed((long) 1, "Teste"));
//		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(feed.getName() + " added"));
	}

	public MenuModel geraSubmenu() {
		 MenuModel model = new DefaultMenuModel();
		 DefaultSubMenu subMenu = new DefaultSubMenu("some menu");
		 DefaultMenuItem menuItem = new DefaultMenuItem("some item");
		 subMenu.addElement(menuItem);
		 model.generateUniqueIds();
		 return model;
	}
		 
	public List<Feed> getSelectedFeeds() {
		return selectedFeeds;
	}

	public void setSelectedFeeds(List<Feed> selectedFeeds) {
		this.selectedFeeds = selectedFeeds;
	}

	public MenuModel getMenuModel() {	
		return menuModel;
	}

	public void setMenuModel(MenuModel menuModel) {
		this.menuModel = menuModel;
	}
	
	public void onBrowser(){
		Map<String, String> requestParamMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		if(requestParamMap.containsKey("yourArg")){
			String yourValue = requestParamMap.get("yourArg");
			System.out.println("Value " + yourValue);
		}
	}
	
	public void confirmFeedAdd() throws XPathExpressionException, IllegalArgumentException, TransformerException, TransformerFactoryConfigurationError{
		for(int a = 0 ; a < feeds.size() ; a++){
			if(feeds.get(a).getId().equals(idSelectedFeed)){
				selectedFeeds.add(feeds.get(a));
				break;
			}
		}
		for(int a = 0; a < selectedFeeds.size() ; a++){
			System.out.println(selectedFeeds.get(a).getName());
		}
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		System.out.println(request.getRequestURL());
		showXML(createEmml());
	}
	
	public EMMLDocument createEmml() throws XPathExpressionException{
		EMMLDocument emmlDocument = new EMMLDocument();
		
	//	HttpRequest http = FacesContext.getCurrentInstance().getExternalContext().getContext();
	//	http.
		emmlDocument.NewInput("entrada2", "boolean" , null, null, "miau");
		emmlDocument.AddNewGlobalVariable("cachorro", "boolean", null);
		emmlDocument.CreateOutput("saidadosmano", "int", null, null, "labeldosmano");
	//	emmlDocument.Group("name", "groupResult");
		emmlDocument.PrintAllNodes();
		emmlDocument.SaveDoc();
		return emmlDocument;
	}
	
	public void showXML(EMMLDocument emmlDocument) throws IllegalArgumentException, TransformerException, TransformerFactoryConfigurationError{
//		Feed feed = new Feed(new Long(20), "teste");
//		XStream stream = new XStream(new DomDriver());
//		System.out.println(stream.toXML(feed));		
//		setXmlFeed(stream.toXML(feed));
		setXmlFeed(emmlDocument.xmlDocToString());
		RequestContext.getCurrentInstance().update("showXML");
	}

	public String getXmlFeed() {
		return xmlFeed;
	}

	public void setXmlFeed(String xmlFeed) {
		this.xmlFeed = xmlFeed;
	}

	public boolean isTest() {
		return test;
	}

	public void setTest(boolean test) {
		this.test = test;
	}
}
